﻿Imports Entities
Imports Business
Imports DataAccess
Public Class ParametrosForm
    Private _Paramet As New parametros

    Private Sub ParametrosForm_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        CargarParametros()
    End Sub
    
    Private Sub CargarParametros()
        Util.CurrentParam = ParametrosBL.GetParametros
        Me._Paramet = Util.CurrentParam
        If Not IsNothing(_Paramet) Then
            With _Paramet
                tRut.Text = .RUTEMPRESA
                tNombre.Text = .NOMBREEMPRESA
                tGiro.Text = .GIROEMPRESA
                tRazonSocial.Text = .RAZONSOCIAL
                tDireccion.Text = .DIRECCIONEMPRESA
                tFono.Text = .FONOEMPRESA
                tIVA.Text = .VALORIVA * 100
                If Not IsNothing(.VALORIVASM) Then tIVASM.Text = .VALORIVASM * 100 Else tIVASM.Text = 0
            End With
        Else
            Limpiar()
        End If
    End Sub
    Private Sub Limpiar()
        tRut.Clear()
        tNombre.Clear()
        tGiro.Clear()
        tRazonSocial.Clear()
        tDireccion.Clear()
        tFono.Clear()
        tIVA.Clear()
        tIVASM.Clear()
    End Sub

    Private Sub bAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAceptar.Click
        If tRut.Text <> "  .   .   -" Then
            If Not Util.ValidaRut(tRut.Text) Then
                Util.Mensaje("El Rut Ingresado no es Correcto")
                tRut.Focus()
                Exit Sub
            End If
        Else
            Util.Mensaje("Debe Ingresar el Rut de su Empresa")
            tRut.Focus()
            Exit Sub
        End If
        If tNombre.TextLength = 0 Then
            Util.Mensaje("Debe Ingresar el Nombre de su Empresa")
            tNombre.Focus()
            Exit Sub
        End If
        If tIVA.TextLength = 0 Or Val(tIVA.Text) = 0 Then
            Util.Mensaje("Debe Ingresar el Valor Actual del IVA")
            tIVA.Focus()
            Exit Sub

        End If

        Dim para As New parametros
        With para
            .DIRECCIONEMPRESA = tDireccion.Text.Trim
            .FONOEMPRESA = tFono.Text.Trim
            .GIROEMPRESA = tGiro.Text.Trim
            .NOMBREEMPRESA = tNombre.Text.Trim
            .RAZONSOCIAL = tRazonSocial.Text.Trim
            .RUTEMPRESA = tRut.Text
            .VALORIVA = CLng(tIVA.Text.Trim) / 100
            .VALORIVASM = CLng(Val(tIVASM.Text.Trim)) / 100
        End With

        ParametrosBL.Save(para)
        If IsNothing(para) Then
            Util.Mensaje("Error al Guardar Parámetros")
        Else
            Util.Mensaje("Parámetros Guardados Correctamente")

        End If
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub

    Private Sub tRut_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tRut.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tNombre_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tNombre.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tGiro_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tGiro.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tRazonSocial_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tRazonSocial.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tDireccion_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tDireccion.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tFono_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tFono.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tIVA_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tIVA.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tIVASM_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tIVASM.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub
End Class